package com.talos.ums.controller.admin.scopeorganization;

import com.talos.framework.apilog.core.annotation.ApiAccessLog;
import com.talos.framework.common.pojo.CommonResult;
import com.talos.framework.common.pojo.PageParam;
import com.talos.framework.common.pojo.PageResult;
import com.talos.framework.common.util.object.BeanUtils;
import com.talos.framework.excel.core.util.ExcelUtils;
import com.talos.ums.controller.admin.scopeorganization.vo.UmsScopeOrganizationPageReqVO;
import com.talos.ums.controller.admin.scopeorganization.vo.UmsScopeOrganizationRespVO;
import com.talos.ums.controller.admin.scopeorganization.vo.UmsScopeOrganizationSaveReqVO;
import com.talos.ums.dal.dataobject.scopeorganization.UmsScopeOrganizationDO;
import com.talos.ums.service.scopeorganization.UmsScopeOrganizationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.talos.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.talos.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 经营范围组织关联表")
@RestController
@RequestMapping("/ums/scope-organization")
@Validated
public class UmsScopeOrganizationController {

    @Resource
    private UmsScopeOrganizationService scopeOrganizationService;

    @PostMapping("/create")
    @Operation(summary = "创建经营范围组织关联表")
    @PreAuthorize("@ss.hasPermission('ums:scope-organization:create')")
    public CommonResult<Long> createScopeOrganization(@Valid @RequestBody UmsScopeOrganizationSaveReqVO createReqVO) {
        return success(scopeOrganizationService.createScopeOrganization(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新经营范围组织关联表")
    @PreAuthorize("@ss.hasPermission('ums:scope-organization:update')")
    public CommonResult<Boolean> updateScopeOrganization(@Valid @RequestBody UmsScopeOrganizationSaveReqVO updateReqVO) {
        scopeOrganizationService.updateScopeOrganization(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除经营范围组织关联表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ums:scope-organization:delete')")
    public CommonResult<Boolean> deleteScopeOrganization(@RequestParam("id") Long id) {
        scopeOrganizationService.deleteScopeOrganization(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得经营范围组织关联表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ums:scope-organization:query')")
    public CommonResult<UmsScopeOrganizationRespVO> getScopeOrganization(@RequestParam("id") Long id) {
        UmsScopeOrganizationDO scopeOrganization = scopeOrganizationService.getScopeOrganization(id);
        return success(BeanUtils.toBean(scopeOrganization, UmsScopeOrganizationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得经营范围组织关联表分页")
    @PreAuthorize("@ss.hasPermission('ums:scope-organization:query')")
    public CommonResult<PageResult<UmsScopeOrganizationRespVO>> getScopeOrganizationPage(@Valid UmsScopeOrganizationPageReqVO pageReqVO) {
        PageResult<UmsScopeOrganizationDO> pageResult = scopeOrganizationService.getScopeOrganizationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UmsScopeOrganizationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出经营范围组织关联表 Excel")
    @PreAuthorize("@ss.hasPermission('ums:scope-organization:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportScopeOrganizationExcel(@Valid UmsScopeOrganizationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UmsScopeOrganizationDO> list = scopeOrganizationService.getScopeOrganizationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "经营范围组织关联表.xls", "数据", UmsScopeOrganizationRespVO.class,
                        BeanUtils.toBean(list, UmsScopeOrganizationRespVO.class));
    }

}
